//
//  TripItemCell.m
//  Traveling
//
//  Created by 曹秀云 on 15/6/5.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "TripItemCell.h"

#import "TripOrderResult.h"


@interface TripItemCell()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *contentIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *useTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *roadLabel;

@end


@implementation TripItemCell

+(instancetype)cellWithTableView:(UITableView *)tableView{
    static  NSString  *CellIdentiferId = @"TripItemCell";
    TripItemCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"TripItemCell" owner:nil options:nil];
        cell = [nibs lastObject];
    }
    return cell;
}


- (void)awakeFromNib {
    
}

-(void)initData:(TripOrderResult *)result{
    [self.titleLabel setText:result.plan_name];
    [self.contentIconImageView sd_setImageWithURL:[NSURL URLWithString:result.title_img]];
    // 2015-06-21 08:00:00
    
    NSString *string = result.plan_start;
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *inputDate = [inputFormatter dateFromString:string];
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *date = [inputFormatter stringFromDate:inputDate];
    [self.dateLabel setText:date];
        
    NSString *toDate = result.plan_end;
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *outDate = [outputFormatter dateFromString:toDate];
    [outputFormatter setDateFormat:@"yyyy-MM-dd"];
    
       //需要手动计算时间差
    NSInteger count= [self daysWithStart:inputDate toDate:outDate];
    NSString *compare = [NSString stringWithFormat: @"为期%ld天", (long)count];
    [self.useTimeLabel setText:compare];
    [self.roadLabel setText:result.des];
}

-(NSInteger)daysWithStart:(NSDate *)startDate toDate:(NSDate *)toDate{
    NSCalendar *canlendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [canlendar components:NSCalendarUnitDay fromDate:startDate toDate:toDate options:0];
    return components.day;
}


-(NSString *)dateString:(NSString *)dataStr{
    NSString *string = dataStr;
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *inputDate = [inputFormatter dateFromString:string];
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *date = [inputFormatter stringFromDate:inputDate];
    return date;
}

@end
